//lcd.c

/* Standard includes */
#include <stdint.h>

/* Scheduler includes */
#include "FreeRTOS.h"
#include "task.h"

/* Application includes */
#include "lcd.h"

const uint8_t lcdDigitTable[18] =
{
    CHAR_0,
    CHAR_1,
    CHAR_2,
    CHAR_3,
    CHAR_4,
    CHAR_5,
    CHAR_6,
    CHAR_7,
    CHAR_8,
    CHAR_9,
    CHAR_A,
    CHAR_B,
    CHAR_C,
    CHAR_D,
    CHAR_E,
    CHAR_F,
    CHAR_MINUS,
    CHAR_SPACE
};


/* function to write to clear LCD memory */
//Inputs: none
//Outputs: none
void clrLCD(void)
{
  //int i = LCD_MEM_OFFSET; 
  
  for( int i = LCD_MEM_OFFSET; i < (LCD_MEM_OFFSET + LCD_MEM_LOC); i++)
  {
    LCDMEM[i] = 0;
  }
}

/* function to write to initialize LCD memory */
//Inputs: none
//Outputs: none
void vInitLCD(void)
{
    int i = 0;

    /* Basic timer setup */
    /* Set ticker to 32768/(256*256) */
    BTCTL = BT_fCLK2_DIV128 | BT_fCLK2_ACLK_DIV256;

    for (i = 0;  i < 20;  i++)
        LCDMEM[i] = 0;

    /* Turn on the COM0-COM3 and R03-R33 pins */
    P5SEL |= (BIT4 | BIT3 | BIT2);
    P5DIR |= (BIT4 | BIT3 | BIT2);

    /* LCD-A controller setup */
    LCDACTL = LCDFREQ_128 | LCD4MUX | LCDSON | LCDON;
    LCDAPCTL0 = LCDS0 | LCDS4 | LCDS8 | LCDS12 | LCDS16 | LCDS20 | LCDS24;
    LCDAPCTL1 = 0;
    LCDAVCTL0 = LCDCPEN;
    LCDAVCTL1 = 1 << 1;
}

/* function to put a segment pattern at a specified position on the LCD display */
//Inputs: ch = segment pattern; pos = position in memory
//Outputs: none
void LCDchar
(
  int ch, 
  int pos
)
{
    /* Put a segment pattern at a specified position on the LCD display */
    LCDMEM[9 - pos] = ch;
}

/* function to convert uint16_t input value to decimal LCD display value */
//Inputs: val = decimal value; pos = position in display
//Outputs: none
void LCDdec
(
  uint16_t val, 
  int pos
)
{
    int i = 2;

    for (i = 2;  i >= 0;  i--)
    {
        LCDchar(lcdDigitTable[val%10], pos + i);
        val /= 10;
    }
}

/* function to initialize LCD ports */
//Inputs: none
//Outputs: none
void vPortsInit(void)
{
 P1OUT = 0;
 P1DIR = 0xFF & ~(BIT0|BIT1);
 P1SEL = BIT1; 
}


/************* Added FreeRTOS functionality **************/

void vStartLCDdisplayTasks( unsigned portBASE_TYPE uxPriority )
{
     
  /* create the task */
  uint8_t valueToLCD = 0; //value to display on LCD
  LCDdec(valueToLCD, 3);
     
  /* Spawn the task */
//  xTaskCreate(  );
   
}
